/*
 * pid.c
 *
 *  Created on: Dec 16, 2008
 *      Author: michael
 */

#include<comlib.h>

/****
 * start background process
 ***/
void onChild(int sig)
{
	pid_t pid;
	int nState;
	while ((pid = waitpid(-1, &nState, WNOHANG)) > 0)
		;
	signal(SIGCHLD, onChild);
}

int InitServer()
{
	pid_t pid;
	// create the child process
	ASSERT((pid=fork())>=0);
	// parent process quit
	if (pid > 0)
	{
		sleep(1);
		exit(0);
	}
	// child process running
	ASSERT((setsid())>=0);//脱离终端
	umask(0);
	signal(SIGINT, SIG_IGN);
	// wait child to exit
	signal(SIGCHLD, onChild);
	return 0;
}
